package com.jcf.springboot.controller.business;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcf.springboot.common.R;
import com.jcf.springboot.entity.business.Actor;
import com.jcf.springboot.service.ActorService;
import com.jcf.springboot.entity.business.Category;
import com.jcf.springboot.service.CategoryService;
import com.jcf.springboot.entity.business.Director;
import com.jcf.springboot.service.DirectorService;
import com.jcf.springboot.entity.business.FilmActor;
import com.jcf.springboot.service.FilmActorService;
import com.jcf.springboot.entity.business.FilmCategory;
import com.jcf.springboot.service.FilmCategoryService;
import com.jcf.springboot.entity.business.FilmDirector;
import com.jcf.springboot.service.FilmDirectorService;
import com.jcf.springboot.entity.business.FilmInfo;
import com.jcf.springboot.entity.business.vo.QueryVo;
import com.jcf.springboot.service.FilmInfoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjj
 * @since 2021-03-25
 */
@CrossOrigin
@RestController
@RequestMapping("/filminfo")
public class FilmInfoController {

    @Resource
    private FilmInfoService filmInfoService;

    @Resource
    private ActorService actorService;

    @Resource
    private DirectorService directorService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private FilmDirectorService filmDirectorService;

    @Resource
    private FilmActorService filmActorService;

    @Resource
    private FilmCategoryService filmCategoryService;

    @Value("${server.port}")
    private String port;

    @PostMapping("getList")
    public R getList(@RequestBody QueryVo queryVo){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("film_show_time");
        List<FilmInfo> all = filmInfoService.getByMoreTable(queryVo);
        Map<String,Object> map = new HashMap<>();
        map.put("value",all);
        if(all != null){

            return R.ok().data(map).message("查询影片列表成功");
        }
        else {
            return R.error().message("查询影片列表失败");
        }

    }

    @GetMapping("getFilmById")
    public R getFilmById(String id){
        FilmInfo filmInfo = filmInfoService.getFilmById(id);
        if(filmInfo != null){
            return R.ok().data(filmInfo).message("获取影片信息成功");
        }
        else{
            return R.error().message("获取影片信息失败");
        }
    }

    @GetMapping("getAgeList")
    public R getAgeList(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("DISTINCT film_age");
        List list = filmInfoService.list(queryWrapper);
        if(list.size()>0){
            return R.ok().data(list).message("查询电影年代列表成功");
        }
        else {
            return R.error().message("查询电影年代列表失败");
        }
    }

    @GetMapping("getListToPlay")
    public R getListToPlay(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("film_status", "待上映");
        queryWrapper.select("film_id","film_name");
        List list = filmInfoService.list(queryWrapper);
        if(list.size() > 0){
            return R.ok().data(list).message("查询待上映影片列表成功");
        }
        else {
            return R.error().message("查询待上映影片列表失败");
        }
    }

    @GetMapping("getListPlaying")
    public R getListPlaying(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("film_status", "上映");
        queryWrapper.select("film_id","film_name");
        List list = filmInfoService.list(queryWrapper);
        if(list!=null){
            return R.ok().data(list).message("查询上映影片列表成功");
        }
        else {
            return R.error().message("查询上映影片列表失败");
        }
    }

    @PostMapping("addFilm")
    public R addFilm(@RequestBody FilmInfo filmInfo,String directorNames,String actorNames,String filmStyles){
        String flag = "";
        boolean save = filmInfoService.save(filmInfo);
        if(save){
            flag = "电影基础信息添加成功";
            String[] directors = directorNames.split(",");
            for (String directorName : directors) {
                QueryWrapper<Director> wrapper = new QueryWrapper<Director>();
                wrapper.eq("director_name", directorName);
                Director thisDirecto = directorService.getOne(wrapper);
                if(thisDirecto == null){
                    Director director = new Director();
                    director.setDirectorName(directorName);
                    boolean saveD = directorService.save(director);
                    System.out.println(director);
                    if(saveD){
                        FilmDirector filmDirector = new FilmDirector();
                        filmDirector.setFilmId(filmInfo.getFilmId());
                        filmDirector.setDirectorId(director.getDirectorId());
                        filmDirectorService.save(filmDirector);

                    }
                }
                else {
                    FilmDirector filmDirector = new FilmDirector();
                    filmDirector.setDirectorId(thisDirecto.getDirectorId());
                    filmDirector.setFilmId(filmInfo.getFilmId());
                    filmDirectorService.save(filmDirector);

                }
            }

            String[] actors = actorNames.split(",");
            for (String actorName : actors) {
                QueryWrapper<Actor> wrapper = new QueryWrapper<Actor>();
                wrapper.eq("actor_name", actorName);
                Actor thisActor = actorService.getOne(wrapper);
                if(thisActor == null){
                    Actor actor = new Actor();
                    actor.setActorName(actorName);
                    boolean saveA = actorService.save(actor);
                    System.out.println(actor);
                    if(saveA){
                        FilmActor filmActor = new FilmActor();
                        filmActor.setFilmId(filmInfo.getFilmId());
                        filmActor.setActorId(actor.getActorId());
                        filmActorService.save(filmActor);

                    }
                }
                else {
                    FilmActor filmActor = new FilmActor();
                    filmActor.setActorId(thisActor.getActorId());
                    filmActor.setFilmId(filmInfo.getFilmId());
                    filmActorService.save(filmActor);

                }

            }

            String[] categorys = filmStyles.split(",");
            for (String categoryName : categorys) {
                QueryWrapper<Category> wrapper = new QueryWrapper<Category>();
                wrapper.eq("cate_name", categoryName);
                Category thisCatory = categoryService.getOne(wrapper);


                        FilmCategory filmCategory = new FilmCategory();
                        filmCategory.setFilmId(filmInfo.getFilmId());
                        filmCategory.setCateId(thisCatory.getCateId());
                       filmCategoryService.save(filmCategory);


                }


            return R.ok().message(flag);




        }
        else {
            return R.error().message("添加影片信息失败");
        }

    }

    @PostMapping("editFilm")
    public R editFilm(@RequestBody FilmInfo filmInfo,String directorNames,String actorNames,String filmStyles){

        String[] directors = directorNames.split(",");
        String[] actors = actorNames.split(",");
        String[] styles = filmStyles.split(",");
        boolean b = filmInfoService.updateById(filmInfo);
        if(!b){
            return R.error().message("修改影片基础信息失败");
        }
        QueryWrapper<FilmDirector> filmDirectorQueryWrapper = new QueryWrapper<>();
        filmDirectorQueryWrapper.eq("film_id", filmInfo.getFilmId());
        boolean remove = filmDirectorService.remove(filmDirectorQueryWrapper);
        if(!remove){
            return R.error().message("删除影片导演关联失败");
        }
        for (String directorName : directors) {
            QueryWrapper<Director> wrapper = new QueryWrapper<>();
            wrapper.eq("director_name", directorName);
            Director one = directorService.getOne(wrapper);
            if(one != null){

                    FilmDirector filmDirector = new FilmDirector();
                    filmDirector.setDirectorId(one.getDirectorId());
                    filmDirector.setFilmId(filmInfo.getFilmId());
                    boolean save = filmDirectorService.save(filmDirector);
                    if(!save){
                        return R.error().message("添加影片导演关联失败");
                    }


            }
            else{
                return R.error().message(directorName+"导演不存在");
            }
        }
        QueryWrapper<FilmActor> filmActorQueryWrapper = new QueryWrapper<>();
        filmActorQueryWrapper.eq("film_id", filmInfo.getFilmId());
        boolean removeActor = filmActorService.remove(filmActorQueryWrapper);
        if(!removeActor){
            return R.error().message("删除影片演员关联失败");
        }
        for (String actorName : actors) {
            QueryWrapper<Actor> wrapper = new QueryWrapper<>();
            wrapper.eq("actor_name", actorName);
            Actor one = actorService.getOne(wrapper);
            if(one != null){
                   FilmActor filmActor = new FilmActor();
                    filmActor.setActorId(one.getActorId());
                    filmActor.setFilmId(filmInfo.getFilmId());
                    boolean save = filmActorService.save(filmActor);
                    if(!save){
                        return R.error().message("添加影片演员关联失败");
                    }


            }
            else{
                return R.error().message(actorName+"演员不存在");
            }
        }
        QueryWrapper<FilmCategory> filmCategoryQueryWrapper = new QueryWrapper<>();
        filmCategoryQueryWrapper.eq("film_id", filmInfo.getFilmId());
        boolean removeFilmCate = filmCategoryService.remove(filmCategoryQueryWrapper);
        if(!removeFilmCate){
            return R.error().message("删除电影分类关联失败");
        }
        for (String style : styles) {
            QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
            categoryQueryWrapper.eq("cate_name", style);
            Category one = categoryService.getOne(categoryQueryWrapper);
            if(one != null){
                FilmCategory filmCategory = new FilmCategory();
                filmCategory.setCateId(one.getCateId());
                filmCategory.setFilmId(filmInfo.getFilmId());
                boolean save = filmCategoryService.save(filmCategory);
                if(!save){
                    return R.error().message("添加电影分类关联失败");
                }
            }



        }
        return R.ok().message("编辑影片成功");
    }


    @DeleteMapping("deleteFilmById")
    public R deleteFilmById(String id){
        boolean b = filmInfoService.removeById(id);
        if(b){
            QueryWrapper<FilmCategory> filmCategoryQueryWrapper = new QueryWrapper<>();
            filmCategoryQueryWrapper.eq("film_id", id);
            boolean remove = filmCategoryService.remove(filmCategoryQueryWrapper);
            if(remove){
                QueryWrapper<FilmDirector> filmDirectorQueryWrapper = new QueryWrapper<>();
                filmDirectorQueryWrapper.eq("film_id", id);
                boolean remove1 = filmDirectorService.remove(filmDirectorQueryWrapper);
                if(remove1){
                    QueryWrapper<FilmActor> filmActorQueryWrapper = new QueryWrapper<>();
                    filmActorQueryWrapper.eq("film_id", id);
                    boolean remove2 = filmActorService.remove(filmActorQueryWrapper);
                    if(remove2){
                        return R.ok().message("删除电影成功");
                    }
                    else{
                        return R.error().message("删除电影演员关联失败");
                    }
                }
                else {
                    return R.error().message("删除电影导演关联失败");
                }
            }
            else {
                return R.error().message("删除电影分类关联失败");
            }
        }
        else{
            return R.error().message("删除电影基础信息失败");
        }
    }


    @RequestMapping("/upload")
    public R upload(MultipartFile file){
        String originalFileName = file.getOriginalFilename();
        String fileName = file.getOriginalFilename().substring(0,originalFileName.lastIndexOf('.'))+new Date().getTime()+file.getOriginalFilename().substring(originalFileName.lastIndexOf('.'));
        String filePath = "E:\\graduationProject\\cinema-system-parent\\service\\service-cinema\\src\\main\\resources\\static\\image\\filmPoster\\";
        File dest = new File(filePath + fileName);

        try {
            file.transferTo(dest);
            String url = "http://localhost:"+port+"/image/"+fileName;
            return R.ok().data("url",url).message("上传电影海报成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error().message("上传电影海报失败");

    }

    @PostMapping("/updateStills")
    public R updateStills(@RequestBody FilmInfo filmInfo){
        boolean b = filmInfoService.updateById(filmInfo);
        if(b){
            return R.ok().message("保存剧照成功");
        }
        else{
            return R.error().message("保存剧照失败");
        }
    }

    @GetMapping("/getStillsById")
    public R getStillsById(String id){
        QueryWrapper<FilmInfo> wrapper = new QueryWrapper<>();
        wrapper.select("stills");
        FilmInfo info = filmInfoService.getById(id);
        if(info != null){
            return R.ok().data(info).message("获取剧照成功");
        }
        else {
            return R.error().message("获取剧照失败");
        }
    }
}

